<template>
    <div class="user-profile-component">
        <div class="d-flex align-items-center">
            <img :src="fitUrl" :alt="user.nickName" class="rounded-circle img-thumbnail">
            <div class="detail ml-2">
                <h6 class="d-block mb-0">{{ user.nickName }}</h6>
                <span class="text-truncate text-muted d-block">{{ user.description }}</span>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { defineProps, watch, ref } from 'vue';

// 接收父组件传递的 props
const props = defineProps({
  user: {
    type: Object,
    required: true,
  },
});
// 用来存储最终生成的 avatar url
const fitUrl = ref('');
// 监听 user.avatar 对象的变化
watch(
  () => props.user.avatar,
  (newAvatar) => {
    if (newAvatar && newAvatar.url) {
      fitUrl.value = newAvatar.url + '?x-oss-process=image/resize,m_pad,h_50,w_50';
    } else {
      // 使用导入的默认头像
      fitUrl.value = require('@/assets/avatar.jpg') + '?x-oss-process=image/resize,m_pad,h_50,w_50';
    }
  },
  { immediate: true } // 组件加载时初始化一次
);
</script>

<style scoped>
/* 你的样式 */
</style>
